<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="姓名"
        style="width: 100px;"
      />
      <el-button type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button
        v-if="isP('DicPersonAdd')"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
      >
        新增
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      stripe
      highlight-current-row
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="id"
        label="id"
        width="100"
      />
      <el-table-column
        prop="name"
        label="姓名"
        width="100"
      />
      <el-table-column
        prop="age"
        label="年龄"
        width="100"
      />
      <el-table-column
        prop="height"
        label="身高"
        width="100"
      />
      <el-table-column
        prop="birth"
        label="生日"
        width="100"
      />
      <el-table-column
        prop="create1"
        label="创建日期"
        width="100"
      />
      <el-table-column
        prop="test"
        label="测试"
        width="100"
      />
      <el-table-column align="center" label="操作" width="240">
        <template slot-scope="scope">
          <el-button
            v-if="isP('DicPersonGet')"
            style="margin-left: 10px"
            type="success"
            size="mini"
            @click="handleInfo(scope.row)"
          >详细
          </el-button>
          <el-button
            v-if="isP('DicPersonUpdate')"
            style="margin-left: 10px"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-if="isP('DicPersonDelete')"
            style="margin-left: 10px"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="listDicPerson"
    />
  </div>
</template>

<script>
import { listDicPerson, deleteDicPerson } from '@/api/dic/person'
import Pagination from '@/components/Pagination/index'
import msgUtil from '@/utils/msgUtil'

export default {
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        name: null,
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.listDicPerson()
  },
  methods: {
    listDicPerson() {
      this.listLoading = true
      listDicPerson(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      this.listDicPerson()
    },
    // 删除
    handleDelete(row) {
      msgUtil.confirm('确认删除该人员字典?').then(async() => {
        await deleteDicPerson({ id: row.id })
        msgUtil.success2('删除成功!')
        this.listDicPerson()
      })
    },
    // 修改
    handleUpdate(row) {
      this.$router.push({
        path: '/dic/person/update',
        query: { id: row.id }
      })
    },
    // 查看详情
    handleInfo(row) {
      this.$router.push({
        path: '/dic/person/info',
        query: { id: row.id }
      })
    },
    // 新增
    handleAdd() {
      this.$router.push({
        path: '/dic/person/add',
        query: {}
      })
    }
  }
}
</script>
